Add a scope `with_location` to Event.

Akinori MUSHA 10 years ago
parent
commit
aceed9ac83

+ 4 - 0
app/models/event.rb

@@ -28,6 +28,10 @@ class Event < ActiveRecord::Base
28 28
     where("expires_at IS NOT NULL AND expires_at < ?", Time.now)
29 29
   }
30 30
 
31
+  scope :with_location, -> {
32
+    where.not(lat: nil).where.not(lng: nil)
33
+  }
34
+
31 35
   # Emit this event again, as a new Event.
32 36
   def reemit!
33 37
     agent.create_event :payload => payload, :lat => lat, :lng => lng

+ 1 - 1
app/views/agents/agent_views/user_location_agent/_show.html.erb

@@ -2,7 +2,7 @@
2 2
 
3 3
 <h3>Recent Event Map</h3>
4 4
 
5
-<% events = @agent.events.where("lat IS NOT null AND lng IS NOT null").order("id desc").limit(500) %>
5
+<% events = @agent.events.with_location.order("id desc").limit(500) %>
6 6
 <% if events.length > 0 %>
7 7
   <div id="map_canvas" style="width:800px; height:800px"></div>
8 8
 

+ 14 - 0
spec/models/event_spec.rb

@@ -1,6 +1,20 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe Event do
4
+  describe ".with_location" do
5
+    it "selects events with location" do
6
+      event = events(:bob_website_agent_event)
7
+      event.lat = 2
8
+      event.lng = 3
9
+      event.save!
10
+      Event.with_location.pluck(:id).should == [event.id]
11
+
12
+      event.lat = nil
13
+      event.save!
14
+      Event.with_location.should be_empty
15
+    end
16
+  end
17
+
4 18
   describe "#reemit" do
5 19
     it "creates a new event identical to itself" do
6 20
       events(:bob_website_agent_event).lat = 2